import * as nodemailer from 'nodemailer';

import { generateRichEmailTemplate } from './Html';

// 创建一个可重用的邮件传输对象
const transporter = nodemailer.createTransport({
  host: 'smtp.qq.com',
  port: 465,
  secure: true, // true for 465, false for other ports
  auth: {
    user: '2580578725@qq.com',
    pass: 'wihotecewykmechd',
  },
});

// 导出一个函数来发送邮件
export const sendEmail = async (
  use_mail: string,
  code: string,
): Promise<void> => {
  const mailContent = generateRichEmailTemplate(use_mail, code);
  try {
    // 发送邮件
    await transporter.sendMail({
      from: '2580578725@qq.com',
      to: use_mail,
      subject: '验证码',
      html: mailContent,
    });
    console.log('邮件已发送');
  } catch (error) {
    console.error('发送邮件时出错:', error);
  }
};
